package org.jeecg.modules.es.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.constant.ElasticSearchConstant;
import org.jeecg.modules.article.entity.Article;
import org.jeecg.modules.article.service.IArticleService;
import org.jeecg.modules.es.service.ElasticSearchService;
import org.jeecg.modules.es.vo.KeywordsVO;
import org.jeecg.modules.es.vo.KnowledgeVO;
import org.jeecg.modules.knowledge.entity.PhilKnowledge;
import org.jeecg.modules.knowledge.service.IPhilKnowledgeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * TODO:
 *
 * @author Zhang Hao yu
 * @date 2020/12/2 18:00
 */
@Api(tags="全文检索")
@RequestMapping("/search")
@RestController
public class ElasticSearchController {

    @Autowired
    private ElasticSearchService elasticSearchService;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IPhilKnowledgeService knowledgeService;

    /**
     * TODO: 低级搜索，，只需要输入一个关键字即可
     *
     *
     * @author ZhangHaoyu
     * @param keywords
     * @param pageNo
     * @param pageSize
     * @return org.jeecg.common.api.vo.Result<?>
     */
    @AutoLog(value = "全文检索-搜索")
    @ApiOperation(value="全文检索-搜索", notes="全文检索-搜索")
    @GetMapping("/search")
    public Result<?> search(@RequestParam("keywords") String keywords,
                            @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        if (pageNo<=1){
            pageNo = 1;
        }
        if (keywords.isEmpty()){
            return Result.error("keywords不能为空");
        }
        if (pageSize <= 0){
            pageSize = 10;
        }

        List<Map<String, Object>> maps = elasticSearchService.searchPage(keywords, pageNo, pageSize, ElasticSearchConstant.PHIL_ARTICLE);
        return Result.OK(maps);
    }


    /**
     * TODO: 高级搜索，，
     *
     *
     * @author ZhangHaoyu
     * @param keywords
     * @param pageNo
     * @param pageSize
     * @return org.jeecg.common.api.vo.Result<?>
     */
    @AutoLog(value = "全文检索-高级搜索")
    @ApiOperation(value="全文检索-高级搜索", notes="全文检索-高级搜索")
    @PostMapping("/majorSearch")
    public Result<?> majorSearch(@RequestBody KeywordsVO keywords,
                                 @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        if (StrUtil.isAllBlank(keywords.getKeywords(),keywords.getTitle(),keywords.getAuthor(),keywords.getContent()))
            return Result.error("参数不能为空");
        if (pageNo<=1){
            pageNo = 1;
        }
        if (pageSize <= 0){
            pageSize = 10;
        }

        List<Map<String, Object>> maps = elasticSearchService.searchPageMajor(keywords, pageNo, pageSize, ElasticSearchConstant.PHIL_KNOWLEDGE);
        return Result.OK(maps);
    }


    @AutoLog(value = "全文检索-管理员后台添加文章")
    @ApiOperation(value="全文检索-管理员后台添加文章", notes="全文检索-管理员后台添加文章")
    @GetMapping("/bulkData")
    public Result<?> addArticle(){
        List<Article> list = articleService.list();
        list.forEach(res->{
            res.setArticleImageString(res.getArticleImageString());
            res.setArticleImage(res.getArticleImage());
        });
        boolean b = false;
        try {
            b = elasticSearchService.bulkData(ElasticSearchConstant.PHIL_ARTICLE, "10s", list);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.OK(b?"插入成功":"插入失败");
    }
    @AutoLog(value = "全文检索-管理员后台添加知识")
    @ApiOperation(value="全文检索-管理员后台添加知识", notes="全文检索-管理员后台添加知识")
    @GetMapping("/bulkData2")
    public Result<?> addKnowledge(){
        List<PhilKnowledge> list = knowledgeService.list();
        List<KnowledgeVO> knowledgeVOList = new ArrayList<>();
        list.forEach(res->{
            res.setKnowledgeImageString(res.getKnowledgeImageString());
            String[] split = res.getKnowledgeKeywords().split(",");
            List<String> strings = Arrays.asList(split);
            KnowledgeVO knowledgeVO = new KnowledgeVO();
            BeanUtils.copyProperties(res,knowledgeVO);
            knowledgeVO.setKeywordList(strings);
            knowledgeVOList.add(knowledgeVO);
        });
        boolean b = false;
        try {
            b = elasticSearchService.bulkData(ElasticSearchConstant.PHIL_KNOWLEDGE, "10s", knowledgeVOList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.OK(b?"插入成功":"插入失败");
    }

}